<?php

require_once('../includes/DB.class.php');
require_once('../includes/Filter.class.php');
require_once('../includes/MultipleTableExporter.class.php');

try{
	$keyname = Filter::forDBInsertion($_GET['keyname']);
	
	$filename = $keyname.'_'.date('YmdHis').'.xls';
	$exp = new MultipleTableExporter($filename);
	//首先查询总表，总表没有什么特殊情况，查询完后将结果中的count和weight全部相加一次作为小结即可完成
$query = "select * from little_main where certificationNumber = '{$keyname}' order by size1, size2, size3";
	DB::query($query);
	
	if(DB::num_rows() > 0){
	$certificationNumber = array();

	$certificationCodification = array();
	
	$sequenceNumber = array();
	 
	$manufactory = array();
	
	$authenticationMark = array();
	
	$steelNumber =  array();
	
	$size1 = array();
	
	$size2 = array();
	
	$size3 = array();
	
	$count = array();
	
	$unitWeight = array();
	
	$weight = array();
	
	$orderNumber = array();
	
	$orderSubitemNumber = array();
	
	$unitPrice = array();
	
	$materialNumber = array();
	
	$consignmentBatch = array();
	
	$sampleBatch = array();
	
	$facheNumber = array();
	
	$purchaseNumber = array();
	
	$shipmentDate = array();
	
	$month = array();
	
	$vehicleNumber = array();
	
	$consignee = array();
	
	$remark = array();
	
	$filename = array();
	
	$uploadTime = array();
		
	$result = DB::getResult();
	
	while($row = $result->fetch_assoc()){
		array_push($certificationNumber, $row['certificationNumber']);
		array_push($certificationCodification, $row['certificationCodification']);
		array_push($sequenceNumber, $row['sequenceNumber']);
		array_push($manufactory, $row['manufactory']);
		array_push($authenticationMark, $row['authenticationMark']);
		array_push($steelNumber, $row['steelNumber']);	
		array_push($size1, $row['size1']);
		array_push($size2, $row['size2']);
		array_push($size3, $row['size3']);
		array_push($unitWeight, $row['unitWeight']);	
		array_push($count, $row['count']);
		array_push($weight, $row['unitWeight']*$row['count']);
		array_push($orderNumber, $row['orderNumber']);
		array_push($orderSubitemNumber, $row['orderSubitemNumber']);
		array_push($unitPrice,$row['unitPrice']);
		array_push($materialNumber, $row['materialNumber']);
		array_push($consignmentBatch, $row['consignmentBatch']);
		array_push($sampleBatch,$row['sampleBatch']);
		array_push($facheNumber,$row['facheNumber']);
		array_push($purchaseNumber,$row['purchaseNumber']);
		array_push($shipmentDate,$row['shipmentDate']);
		array_push($month,$row['month']);
		array_push($vehicleNumber, $row['vehicleNumber']);
		array_push($consignee, $row['consignee']);
		array_push($remark, $row['remark']);
		array_push($filename, $row['filename']);
		array_push($uploadTime, $row['uploadTime']);
	}
	$planSumCount = 0;
	$planSumWeight = 0;
	foreach($count as $c){
		$planSumCount += intval($c);
	}
	foreach($weight as $w){
		$planSumWeight += $w;
	}
	$exp->setMainSumCount($planSumCount);
	$exp->setMainSumWeight($planSumWeight);
	$data = array($certificationNumber,$certificationCodification, $sequenceNumber, 
				  $manufactory, $authenticationMark, $steelNumber, $size1, $size2, $size3, $count,
				  $unitWeight, $weight, $orderNumber, $orderSubitemNumber, $unitPrice,$materialNumber, 
				  $consignmentBatch, $sampleBatch,$facheNumber,$purchaseNumber,$shipmentDate,
				  $month,$vehicleNumber, $consignee, $remark, $filename, $uploadTime);
	$exp->insertMainData($data);
		

/*		
		$smarty->assign('planSumCount', $planSumCount);
		$smarty->assign('planSumWeight', $planSumWeight);
		$smarty->assign('planRowspan', DB::num_rows() + 2);*/
	}else{
		$exp->close();
	}
	
	//然后是对于入库的查询，也是简简单单地只需要查询那些phase为"入库"的发车的项目，然后进行小结
		
$query = "select * from little_ruku where certificationNumber = '{$keyname}' order by certificationNumber";
	DB::query($query);
	
	if(DB::num_rows() > 0){
		$rukuDate = array();
		$certificationNumber = array();
		$certificationCodification = array();
		$consignmentBatch = array();
		$remark1 = array();
		$remark2 = array();
		$filename = array();
		$uploadTime = array();
		$result = DB::getResult();

		while($row = $result->fetch_assoc()){
			array_push($rukuDate, $row['rukuDate']);
			array_push($certificationNumber, $row['certificationNumber']);
			array_push($certificationCodification, $row['certificationCodification']);
			array_push($consignmentBatch, $row['consignmentBatch']);
			array_push($remark1, $row['remark1']);
			array_push($remark2, $row['remark2']);
			array_push($filename, $row['filename']);
			array_push($uploadTime, $row['uploadTime']);
		}
	$data = array($rukuDate, $certificateNumber, $certificationCodification, $consignmentBatch,
				$remark1, $remark2, $filename, $uploadTime);
	$exp->insertRukuData($data);
	}
	//对于出库的， 需要查询发船中的所有和发车中的标志为“出库”的信息，然后进行相加求出小结
	$query = "select * from 
				(
					(select * from little_chuku where certificationNumber = '$keyname') as chukuTable
				  		left join
				  	(select id,filename,uploadTime from little_chuku_file) as chukuFileTable
				  	on (chukuFileTable.id = chukuTable.fileId)
				 )
				 order by certificationNumber";
	
	DB::query($query);
	
	if(DB::num_rows() > 0){
		$chukuDate = array();
		$certificationNumber = array();
		$certificationCodification = array();
		$handler = array();
		$consignmentBatch = array();
		$remark1 = array();
		$remark2 = array();
		$filename = array();
		$uploadTime = array();
		
		$result = DB::getResult();
		
		while($row = $result->fetch_assoc()){
			array_push($chukuDate, $row['chukuDate']);
			array_push($certificationNumber, $row['certificationNumber']);
			array_push($certificationCodification, $row['certificationCodification']);
			array_push($handler, $row['handler']);
			array_push($consignmentBatch, $row['consignmentBatch']);
			array_push($remark1, $row['remark1']);
			array_push($remark2, $row['remark2']);
			array_push($filename, $row['filename']);
			array_push($uploadTime, $row['uploadTime']);
		}
		$data = array($chukuDate, $certificateNumber, $certificationCodification, $handler, $consignmentBatch,
				$remark1, $remark2, $filename, $uploadTime);
		$exp->insertChukuData($data);
	}

	$exp->export();
}catch(Exception $e){
	$smarty = SmartyManager::getSmarty();
	$smarty->assign('errMsg', $e);
	$smarty->assign('errTitle', '查询数据库时出现错误');
	$smarty->display('error.html');
	die();
}

?>